package com.imooc.ecommerce.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.imooc.ecommerce.annotation.IgnoreResponseAdvice;
import com.imooc.ecommerce.service.IJWTService;
import com.imooc.ecommerce.vo.JwtToken;
import com.imooc.ecommerce.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对外暴露的授权服务接口
 * @author zhangshao
 * @date 2023/11/15 23:31
 */
@Slf4j
@RestController
@RequestMapping("/authority")
public class AuthorityController {
    @Autowired
    private IJWTService jwtService;

    /**
     * 从授权中心获取token (其实就是登录功能),且返回信息中没有统一响应的包装
     * @param usernameAndPassword
     * @return
     */
    @IgnoreResponseAdvice
    @PostMapping("/token")
    public JwtToken token(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {
        log.info(" request to get token with param :[{}]", JSON.toJSONString(usernameAndPassword));
        return new JwtToken(
                jwtService.generateToken(usernameAndPassword.getUsername(), usernameAndPassword.getPassword())
        );

    }

    /**
     * 注册用户并返回当前注册用户的 token,即通过授权中心创建用户
     * @param usernameAndPassword
     * @return
     * @throws Exception
     */
    @IgnoreResponseAdvice
    @PostMapping("/register")
    public JwtToken register(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {

        log.info("register user with param : [{}]",JSON.toJSONString(usernameAndPassword));
        return new JwtToken(jwtService.registerUserAndGenerateToken(usernameAndPassword));

    }
}
